import { productPageSelect, productSpecPageSelect } from '@/api/code'
import { getProductSpec, getSpareSpec } from '@/api/goodsInfo'
import { ref } from 'vue'

/**
 * 获取产品下拉选项
 */
export const useGetProductOptions = () => {
  const productOptions = ref<Array<ProductAPIResponse.ProductOption>>([])
  const getProductOptionsParam = ref<ProductAPIRequest.GetProductOptionsParam>({
    limit: 100,
    page: 1,
  })
  const getProductOptionsLoading = ref(false)

  const getProductOptions = async () => {
    try {
      getProductOptionsLoading.value = true
      const res = await productPageSelect(getProductOptionsParam.value)
      getProductOptionsLoading.value = false
      if (res.data.code === 20000) {
        productOptions.value = res.data.data.rows
        return ''
      }
      return res.data.message
    } catch (error) {
      getProductOptionsLoading.value = false
    }
  }

  return {
    productOptions,
    getProductOptionsParam,
    getProductOptionsLoading,
    getProductOptions,
  }
}

/**
 * 获取产品规格下拉选项
 */
export const useGetProductSpecOptions = () => {
  // 产品规格下拉选项
  const productSpecOptions = ref<Array<ProductAPIResponse.ProductSpecOption>>([])

  // 获取产品规格下拉选项请求参数
  const getProductSpecOptionsParam = ref<ProductAPIRequest.GetProductSpecOptionsParam>({
    limit: 100,
    page: 1,
  })

  // 获取产品规格下拉选项加载
  const getProductSpecOptionsLoading = ref(false)

  /**
   * 获取产品规格下拉选项
   */
  const getProductSpecOptions = async (productId: string) => {
    try {
      getProductSpecOptionsLoading.value = true
      const res = await productSpecPageSelect(getProductSpecOptionsParam.value, productId)
      getProductSpecOptionsLoading.value = false
      if (res.data.code === 20000) {
        productSpecOptions.value = res.data.data.rows
        return ''
      }
      return res.data.message
    } catch (error) {
      getProductSpecOptionsLoading.value = false
    }
  }

  return {
    productSpecOptions,
    getProductSpecOptionsParam,
    getProductSpecOptionsLoading,
    getProductSpecOptions,
  }
}

/**
 * 获取商品规格
 */
export const useGetGoodSpecService = () => {
  // 获取规格列表加载
  const getGoodSpecLoading = ref(false)

  // 获取规格请求参数
  const getGoodSpecParam = ref<GoodAPIRequest.GetGoodSpecParam>({
    name: '',
    code: '',
    limit: 50,
    page: 1,
  })

  // 规格列表总数
  const goodSpecTotal = ref(0)

  // 规格列表
  const goodSpecList = ref<Array<ProductAPIResponse.ProductSpec>>([])

  /**
   * 获取产品规格列表
   * @param productId  - 产品id
   */
  const getProductSpecList = async (type: 'product' | 'material', productId: string) => {
    try {
      const res =
        type === 'material'
          ? await getSpareSpec(getGoodSpecParam.value, productId)
          : await getProductSpec(getGoodSpecParam.value, productId)
      if (res.data.code === 20000) {
        goodSpecList.value = res.data.data.rows
        goodSpecTotal.value = Number(res.data.data.total)
      }
    } catch (error) {
      // error
    }
  }

  return {
    goodSpecList,
    goodSpecTotal,
    getGoodSpecParam,
    getGoodSpecLoading,
    getProductSpecList,
  }
}
